package com.offline.module.goods.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.offdissys.common.core.constant.ResultEnum;
import com.offdissys.common.core.lang.Assert;
import com.offdissys.common.db.page.PageParam;
import com.offline.module.goods.dao.goods.GoodsMapper;
import com.offline.module.goods.entity.GoodsInfo;
import com.offline.module.goods.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author ZhangHaiWen
 * @title: GoodsServiceImpl
 * @projectName mybatisplustest
 * @description: 商品信息
 * @date 2021/12/1315:54
 */
@Slf4j
@Service(value = "goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public GoodsInfo create(GoodsInfo goodsInfo) {
        goodsInfo.setAddTime(new Date());
        goodsMapper.insert(goodsInfo);
        GoodsInfo goodsInfo1 = goodsMapper._selectById(goodsInfo.getGoodsId());
        if(ObjectUtil.isNotEmpty(goodsInfo1)){
            System.out.println("你好");
        }
        return goodsInfo;
    }

    /**
     * 商品列表
     * @return
     */
    @Override
    public List<GoodsInfo> goodsInfos() {
        return goodsMapper.selectList(null);
    }

    /**
     * 商品列表 分页
     * @param pageParam
     * @return
     */
    @Override
    public IPage<GoodsInfo> list(PageParam<GoodsInfo> pageParam) {
        GoodsInfo param = pageParam.getParam();
        // 传递Page对象 之后可以动态的获取所有的分页数据
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<GoodsInfo>();
        // 判断商品名称是否为空
        Optional<GoodsInfo> opt = Optional.ofNullable(param);
        // 测试是否为true,如果为true则继续
        Assert.isTrue(opt.isPresent(), ResultEnum.SYS_ERROR);
        // 查找商品名称
        queryWrapper.eq(StringUtils.isNoneBlank(param.getGoodsName()),"goods_name",param.getGoodsName());
        // 降序排列
        queryWrapper.orderByDesc("goods_id","sale_num");

        IPage<GoodsInfo> iPage = goodsMapper.selectPage(pageParam.getPageOjb(), queryWrapper);
        return iPage;
    }
}
